package cn.fboost.mtos.controller;

import cn.fboost.mtos.annotation.RoleIs;
import cn.fboost.mtos.dto.request.*;
import cn.fboost.mtos.dto.Response;
import cn.fboost.mtos.dto.internal.Session;
import cn.fboost.mtos.service.OrderService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 订单控制器
 */
@RestController
@RequestMapping("order")
public class OrderController {
    private final OrderService orderService;

    public OrderController(OrderService orderService) {
        this.orderService = orderService;
    }

    /**
     * 获取配送费
     *
     * @return {@link Response}
     */
    @GetMapping("delivery/fee")
    public Response getDeliveryFee() {
        return orderService.getDeliveryFee();
    }

    /**
     * 获取订单列表
     *
     * @param request {@link OrderListRequest}
     * @return {@link Response}
     */
    @PostMapping("list")
    public Response list(@RequestBody @Validated OrderListRequest request) {
        return orderService.list(request);
    }

    /**
     * 获取订单详情
     *
     * @param request {@link OrderDetailRequest}
     * @return {@link Response}
     */
    @PostMapping("detail")
    public Response detail(@RequestBody @Validated OrderDetailRequest request) {
        return orderService.detail(request);
    }

    /**
     * 创建订单 - 用户接口
     *
     * @param request {@link OrderCreateRequest}
     * @return {@link Response}
     */
    @RoleIs({Session.Role.USER})
    @PostMapping("create")
    public Response create(@RequestBody @Validated OrderCreateRequest request) {
        return orderService.create(request);
    }

    /**
     * 支付订单 - 用户接口
     *
     * @param request {@link OrderPayRequest}
     * @return {@link Response}
     */
    @RoleIs({Session.Role.USER})
    @PostMapping("pay")
    public Response pay(@RequestBody @Validated OrderPayRequest request) {
        return orderService.pay(request);
    }

    /**
     * 取消订单 - 用户接口
     *
     * @param request {@link OrderCancelRequest}
     * @return {@link Response}
     */
    @RoleIs({Session.Role.USER})
    @PostMapping("cancel")
    public Response cancel(@RequestBody @Validated OrderCancelRequest request) {
        return orderService.cancel(request);
    }

    /**
     * 确认收货 - 用户接口
     *
     * @param request {@link OrderFinishRequest}
     * @return {@link Response}
     */
    @RoleIs({Session.Role.USER})
    @PostMapping("finish")
    public Response finish(@RequestBody @Validated OrderFinishRequest request) {
        return orderService.finish(request);
    }

    /**
     * 评价订单 - 用户接口
     *
     * @param request {@link OrderRateRequest}
     * @return {@link Response}
     */
    @RoleIs({Session.Role.USER})
    @PostMapping("rate")
    public Response comment(@RequestBody @Validated OrderRateRequest request) {
        return orderService.rate(request);
    }

    /**
     * 确认接单 - 管理员接口
     *
     * @param request {@link OrderConfirmRequest}
     * @return {@link Response}
     */
    @RoleIs({Session.Role.ADMIN})
    @PostMapping("confirm")
    public Response confirm(@RequestBody @Validated OrderConfirmRequest request) {
        return orderService.confirm(request);
    }

    /**
     * 订单配送 - 管理员接口
     *
     * @param request {@link OrderShipRequest}
     * @return {@link Response}
     */
    @RoleIs({Session.Role.ADMIN})
    @PostMapping("ship")
    public Response ship(@RequestBody @Validated OrderShipRequest request) {
        return orderService.ship(request);
    }

    /**
     * 退单 - 管理员接口
     *
     * @param request {@link OrderRefundRequest}
     * @return {@link Response}
     */
    @RoleIs({Session.Role.ADMIN})
    @PostMapping("refund")
    public Response refund(@RequestBody @Validated OrderRefundRequest request) {
        return orderService.refund(request);
    }

    /**
     * 查看对应订单状态的订单数量
     *
     * @param status 订单状态
     * @return {@link Response}
     */
    @RoleIs({Session.Role.ADMIN})
    @GetMapping("count/status/{status}")
    public Response statusCount(@PathVariable Integer status) {
        return orderService.paidOrderCount(status);
    }
}
